(function(pageName){
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),
      docType = 1,
      newsId = urlParams['newsId'],
      id = urlParams['id'];
      if (id == '0'){
        id = newsId;
      }
    if ($('#j-screen-'+pageName+id).length) {
      var pageScreen = $('#j-screen-'+pageName+id);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $('<div class="j-pagescreen j-pagescreen-detail" id="j-screen-'+pageName+id+'"></div>');
    $('body').append(pageScreen);

    detailShow();
    // 获取新闻
    var detailData = {};
    function detailShow(){
      if (id) {
        sugarcane.ajaxApi({
          "url": "/indexing/getIndexingDetail",
          'type': 'post',
          "data": {"id": +id},
          "callback": function(res){
            if (res['code'] == 0) {
              detailData = res['data'];
              if(res.data.docType){
                docType = res.data.docType;
              }
              //document.title = detailData['title'];
              sugarcane.twoShare({
                "id": res.data.id,
                "title": res.data.title,
                "desc": '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html',
                "imgUrl": res.data.shareImage,
                "link": res.data.shareUrl,
                "docType": res.data.docType
              });
              // 标题
              pageScreen.append(newGrapes["newstitle"].init({"title":detailData['title']}));
              var contentTpl = $('<div class="tag-content"></div>')
              var dataArr = [
                {
                  'text': '标题',
                  'key': 'title'
                },{
                  'text': 'URL',
                  'key': 'keyLink'
                },{
                  'text': '类别',
                  'key': 'categoryNameList'
                },{
                  'text': '来源',
                  'key': 'sourceName'
                },{
                  'text': '稿件内容',
                  'key': 'content'
                }
              ]
              dataArr.forEach(function(item){
                var _content = res.data[item['key']] || '';
                console.log(_content)
                var _part = `<div class="part">
                  <p class="title">`+item.text+`</p>
                  <p class="content">`+ _content+`</p>
                </div>`;
                contentTpl.append(_part);
              })
              pageScreen.append(contentTpl);
            } else {
              showerrorView();
              if(!sugarcane['app']['isShare']){
                peach.hiddenAppbar();
              }
              // peach.alert(res['message']);
            }
          }
        });
      }
    }
   
   

    function showerrorView(){
      var clientHeight = document.documentElement.clientHeight;
      pageScreen.css('position','relative').css('height',clientHeight+'px');
      var errorviewObj = $(`<div class="app-errorview">
          <img class="img" src="/static/img/isempty.png">
        <p class="error-tip">暂时没有数据</p>
      </div>`);
       pageScreen.append(errorviewObj);
    }
  }

  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('tagdetail'));
